<%--
  Created by IntelliJ IDEA.
  User: 15890741124
  Date: 2022/8/9
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>详细信息</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
</head>
<body>
<%--查询--%>
<form class="layui-form" action="" style="margin-top: 20px">
    <div class="layui-inline">
        <label class="layui-form-label">测试名称</label>
        <div class="layui-input-block">
            <input type="text" name="testName" placeholder="请输入问题" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="createBy" placeholder="请输入创建人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button class="layui-btn layui-btn-primary" id="reset" type="reset">重置</button>
        </div>
    </div>
</form>
<%--表格--%>
<table id="demo" lay-filter="test"></table>
<%--表格右侧标签--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn  layui-btn-xs" lay-event="detail">查看二维码</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除 </a>
</script>
<%--表格头标签--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="del">删除</button>
    </div>
</script>
<%-- 添加表单  --%>
<form class="layui-form" action="" lay-filter="addFromFilter" id="addForm"  style="display: none" enctype="multipart/form-data">
   <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">测试名称</label>
        <div class="layui-input-block">
            <input type="text" name="testName" required lay-verify="required" placeholder="请输入测试名称" autocomplete="off"
                   class="layui-input" id="testNameAdd"><span style="color: red"></span>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block">
            <input type="text" name="testBegin" lay-verify="required" placeholder="请输入开始时间" autocomplete="off"
                   class="layui-input" id="beginTime">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block">
            <input type="text" name="testEnd" lay-verify="required" placeholder="请输入结束时间" autocomplete="off"
                   class="layui-input" id="endTime">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark"  placeholder="请输入内容" autocomplete="off" required lay-verify="remark"
                   class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addUserFilter" id="addSub">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%-- 修改表单 根据ID修改问题--%>
<form class="layui-form" action="" lay-filter="updateFromFilter" id="updateForm" style="display: none">
    <input type="hidden" name="id" id="id">
    <div class="layui-form-item">
        <label class="layui-form-label">测试名称</label>
        <div class="layui-input-block">
            <input type="text" name="testName" required lay-verify="required" placeholder="请输入测试名称" autocomplete="off"
                   class="layui-input" id="testNameUpdate"><span style="color: red"></span>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block">
            <input type="text" name="testBegin" lay-verify="required" placeholder="请输入开始时间" autocomplete="off"
                   class="layui-input" id="upBegin">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block">
            <input type="text" name="testEnd" lay-verify="required" placeholder="请输入结束时间" autocomplete="off"
                   class="layui-input" id="upEnd">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark"  placeholder="请输入内容" autocomplete="off" required  lay-verify="remark"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateUserFilter" id="subUp" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%--二维码--%>
<div id="qrcode" style="width:150px; height:150px; display: none; margin-left: 50px;margin-top: 20px"></div>
</body>
<script>

    layui.use(['table', 'form', 'upload','laydate','jquery'], function () {
        var table = layui.table;
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var $=layui.jquery;
        table.render({
            elem: '#demo'
            , height: 550
            , url: '${pageContext.request.contextPath}/testPlanFpa/query.do'
            , dataType: 'json'
            , page: true //开启分页
            // ,toolbar:'default'
            , limits: [5, 10, 15, 20, 25, 30, 100, 200]//将每页几个写在这里，不想默认10个每页
            , autoSort: true//关闭自动排序
            , cols: [[
                {checkbox: 'checkbox', fixed: 'left'}//左侧复选框
                , {field: 'id', title: 'ID', width: 120, sort: true}
                , {field: 'testName', title: '测试名称', width: 120}
                , {field: 'testCode', title: '测试邀请码', width: 120}
                , {field: 'testBegin', title: '开始时间', width: 120}
                , {field: 'testEnd', title: '结束时间', width: 120}
                , {field: 'createTime', title: '创建时间', width: 120}
                , {field: 'createBy', title: '创建者', width: 120}
                , {field: 'updateTime', title: '修改时间', width: 120}
                , {field: 'updateBy', title: '修改者', width: 120}
                , {field: 'remark', title: '备注', width: 120}
                //右侧操作栏
                , {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
            ]]
            , toolbar: "#toolbarDemo"//头部操作栏
        });



        //触发头部工具栏
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据

            switch (obj.event) {
                case "del":
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i = 0; i < data.length; i++) {
                            ids[i] = data[i].id;
                        }
                        layer.confirm('真的删除行么', function (index) {
                            $.ajax({
                                url: "${pageContext.request.contextPath}/testPlanFpa/delete.do",
                                data: {id: ids.join(",")},
                                //响应的数据类型
                                dataType: "JSON",
                                type: "POST"
                                //异步请求
                                // async: true,
                                //成功的回调
                                , success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();//关闭所有
                                        //刷新表格数据
                                        table.reload('demo', {
                                            page: {
                                                curr: 1
                                                //重新从第 1 页开始
                                            }
                                        });

                                    }
                                    layer.msg(data.msg);
                                }
                            });

                        });
                    }
                    break;
                case "add":
                    //将选择的admin填入到表单中
                    layer.open({//弹出添加框
                        type: 1,
                        content: $("#addForm"),
                        area: ('500px', '600px')
                    });
                    laydate.render({
                        elem: '#beginTime'
                        , type: 'datetime'
                        , value: new Date()
                        , isInitValue: true
                    });
                    var now = new Date().getTime();
                    laydate.render({
                        elem: '#endTime'
                        , type: 'datetime'
                        , value: new Date(now + 2 * 86400 * 1000)
                        , isInitValue: true
                    });
                    break;
                default:
                    break;
            }
        });

        //提交添加页面
        form.on('submit(addUserFilter)', function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/testPlanFpa/insert.do",
                type: "POST",
                dataType: "JSON",
                //data:data.field,
                //contentType:"application/json;charset=UTF-8",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        table.reload('demo', {
                            page: {
                                curr: 1
                                //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }

                }
            })
            // document.getElementById("addForm").reset();
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //对问题绑定失去焦点事件，发送ajax请求
        $("#testNameAdd").blur(function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/testPlanFpa/repetitionQusetion.do",
                data: {testName: $("#testNameAdd").val()},
                dataType: "json",
                type: "get",
                success: function (re) {
                    if (re.code == 0) {
                        $("#testNameAdd").next().html(re.msg);
                        $('#addSub').removeClass("layui-btn-disabled").attr("disabled", false);
                    } else {
                        $("#testNameAdd").next().html(re.msg);
                        $('#addSub').addClass("layui-btn-disabled").attr("disabled", true);
                    }
                }
            })
        })
        //监听右侧工具栏事件
        table.on('tool(test)', function (obj) {
            //当前页数据
            var demo = table.cache["demo"];
            switch (obj.event) {
                case 'detail':
                    layer.open({
                        title: '查看二维码',
                        skin: "layui-layer-molv",//弹出层=皮肤样式
                        type: 1,
                        content: $("#qrcode"),
                        area: ['300px', '300px']
                    })
                    break;
                case "del":
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/testPlanFpa/delete.do",
                            data: {id: obj.data.id},
                            //响应的数据类型
                            dataType: "JSON",
                            type: "POST"
                            //异步请求
                            // async: true,
                            //成功的回调
                            , success: function (data) {
                                if (data.code == 0) {
                                    layer.closeAll();
                                    //刷新表格数据
                                    table.reload('demo', {
                                        page: {
                                            curr: 1
                                            //重新从第 1 页开始
                                        }
                                    });
                                }
                              /*  layer.msg(data.msg);*/
                            }
                        });
                    });
                    break;
                case "edit":
                    //将选择的admin填入到表单中
                    form.val("updateFromFilter", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                        "id": obj.data.id, // "name": "value"
                        "testName":obj. data.testName, // "name": "value"
                        "remark": obj.data.remark, // "name": "value"
                    });
                    //日期回显
                    laydate.render({
                        elem: '#upBegin'
                        , type: 'datetime'
                        , value:obj.data.testBegin
                        , isInitValue: true
                    });
                    laydate.render({
                        elem: '#upEnd'
                        , type: 'datetime'
                        , value: obj.data.testEnd
                        , isInitValue: true
                    });
                    layer.open({//弹出修改框
                        type: 1,
                        content: $("#updateForm"),
                        area: ('500px', '600px')
                    });
                    break;
                default:
                    break;
            }
        });
        //提交修改页面
        form.on('submit(updateUserFilter)', function (data) {
            //提交表单数据
            $.ajax({
                url: "${pageContext.request.contextPath}/testPlanFpa/update.do"
                , data: data.field
                , dataType: "json"
                , type: "POST"
                , success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        table.reload('demo', {
                           /* page: {
                                curr: 1
                                //重新从第 1 页开始
                            }*/
                        });
                    }
                    layer.msg(data.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        /**
         * 修改判断问题是否重复
         * @constructor
         */
        $("#testNameUpdate").blur(function (){
            $.ajax({
                url: "${pageContext.request.contextPath}/testPlanFpa/repetitionQusetion.do",
                data: {testName: $("#testNameUpdate").val(), id: $("#id").val()},
                dataType: "json",
                type: "get",
                success: function (re) {
                    if (re.code == 0) {
                        $("#testNameUpdate").next().html(re.msg);
                        $('#subUp').removeClass("layui-btn-disabled").attr("disabled", false);
                    } else {
                        $("#testNameUpdate").next().html(re.msg);
                        $('#subUp').addClass("layui-btn-disabled").attr("disabled", true);
                    }
                }
            })
        })
/*---------------------------查询--------------------------*/

        //查询问题
        form.on('submit(formDemo)', function (data) {
            table.reload('demo', {
                where: {
                    //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    testName: data.field.testName //排序字段-->查询问题
                    , createBy: data.field.createBy//排序方式 --> 查询创建人
                }
                , page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //重置查询条件
        $("#reset").click(function () {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    testName: "" //第一个查询条件
                    , createBy: ""//第二个查询条件
                },
                page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
        });

    });

    /*-------------------------------------二维码渲染----------------------------------------*/

    layui.config({                  //配置layui插件
        base: '${pageContext.request.contextPath}/static/layui/lay/modules/' //配置 layui 第三方扩展组件存放的  目录（不包含文件名）
    }).extend({
        qrcode: 'qrcode'        //前边是方法名，后边是文件名
    }).use(['qrcode'], function () {   //使用插件
        var $ = layui.$;
        $('#qrcode').qrcode({           //调用方法
                                        //这两个属性必须写
            render: "canvas",  //table or canvas     二维码生成的两种方式 canvas是生成图片 table是黑白格子组成的二维码
            text: "http://192.168.41.25:8080/${pageContext.request.contextPath}/views/front/loginTest.jsp",  //二维码的内容

            //宽高和颜色可以不设置
            width: 200,
            height: 200,
            foreground: "#000000",  //二维码上层颜色
            background: "#FFF"      //背景颜色
        });
    });
</script>
</html>
